
package com.app.megaprosoft.DB;

import java.sql.*;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ItemPrice {
	 ResultSet rs = null;
	 SQLiteDBClass myDb;
	 SQLiteDatabase db;
	 Context context;
	 private static final String TABLE_ItemPrice = "_ItemPrice";
	 public Context getContext() {
		return context;
	 }

	 public void setContext(Context context) {
		this.context = context;
	 }
	private String UsrOpt;
	private String OID;
	private String CarTypeOID;
	private String CarSizeOID;
	private String CarGroupOID;
	private String ItemOID;
	private String IsActive;
	private String IsDeleted;
    
	double ItemPrice = 0;
	
	//Param
	private String ParamItemCode;
	private String ParamItemGroupOID;
	private String ParamItemGroupMainOID;
	private String ParamItemOID;
	
    public ItemPrice() {
		
    }
	
	//Generate Get Set 
	public String getCarTypeOID() {
		return CarTypeOID;
	}

	public String getIsActive() {
		return IsActive;
	}

	public void setIsActive(String isActive) {
		IsActive = isActive;
	}

	public String getIsDeleted() {
		return IsDeleted;
	}

	public void setIsDeleted(String isDeleted) {
		IsDeleted = isDeleted;
	}

	public String getUsrOpt() {
		return UsrOpt;
	}

	public void setUsrOpt(String usrOpt) {
		UsrOpt = usrOpt;
	}

	public String getOID() {
		return OID;
	}

	public void setOID(String oID) {
		OID = oID;
	}

	public void setCarTypeOID(String carTypeOID) {
		CarTypeOID = carTypeOID;
	}

	public String getCarSizeOID() {
		return CarSizeOID;
	}

	public void setCarSizeOID(String carSizeOID) {
		CarSizeOID = carSizeOID;
	}

	public String getCarGroupOID() {
		return CarGroupOID;
	}

	public void setCarGroupOID(String carGroupOID) {
		CarGroupOID = carGroupOID;
	}

	public String getItemOID() {
		return ItemOID;
	}

	public void setItemOID(String itemOID) {
		ItemOID = itemOID;
	}

	public double getItemPrice() {
		return ItemPrice;
	}

	public void setItemPrice(double itemPrice) {
		ItemPrice = itemPrice;
	}
	
	//Param
	public String getParamItemCode() {
		return ParamItemCode;
	}

	public void setParamItemCode(String paramItemCode) {
		ParamItemCode = paramItemCode;
	}

	public String getParamItemGroupOID() {
		return ParamItemGroupOID;
	}

	public void setParamItemGroupOID(String paramItemGroupOID) {
		ParamItemGroupOID = paramItemGroupOID;
	}

	public String getParamItemGroupMainOID() {
		return ParamItemGroupMainOID;
	}

	public void setParamItemGroupMainOID(String paramItemGroupMainOID) {
		ParamItemGroupMainOID = paramItemGroupMainOID;
	}

	public String getParamItemOID() {
		return ParamItemOID;
	}

	public void setParamItemOID(String paramItemOID) {
		ParamItemOID = paramItemOID;
	}

	public ResultSet selectItemPrice() throws Exception {
		 Connect con = new Connect(); 
		String StrSql = "SELECT	 [OID]," +
								"[LastModified]," +
								"[ModifyDate]," +
								"[ModifyTime]," +
								"[ModifiedBy]," +
								"[UserModify]," +
								"[CreatedDate]," +
								"[CreateDate]," +
								"[CreateTime]," +
								"[CreatedBy]," +
								"[UserCreate]," +
								"[IsActive]," +
								"[IsDeleted]," +
								
								"[CarTypeOID]," +
								"[CarSizeOID]," +
								"[CarGroupOID]," +
								"[ItemOID]," +
								"[ItemPrice]," +
								
								"[CarGroupName]," +
								"[CarSizeName]," +
								"[CarTypeName]," +
								"[ItemCode]," +
								"[ItemName]," +
								
								"[ItemGroupCode]," +
								"[ItemGroupDesc]," +
								"[ItemGroupOID]," +
								"[ItemGroupMainCode]," +
								"[ItemGroupMainDesc]," +
								
								"[ItemGroupMainOID]," +
								"[ItemUnitDesc] " +
								
						"FROM 	[dbo].[udfItemPrice]() " +
						
						"WHERE 	[IsDeleted] = '0' " ;
		
		if (getParamItemOID() != null) {
			StrSql += "AND [ItemOID] = '" + getParamItemOID() + "' ";
		}
		
		if (getParamItemCode() != null) {
			StrSql += "AND ([ItemCode] LIKE '%" + getParamItemCode() + "%' OR [ItemName] LIKE '%" + getParamItemCode() + "%') ";
		}
		
		if (getParamItemGroupOID() != null) {
			StrSql += "AND [ItemGroupOID] = '" + getParamItemGroupOID() + "' ";
		}else if (getParamItemGroupMainOID() != null) {
			StrSql += "AND [ItemGroupMainOID] = '" + getParamItemGroupMainOID() + "' ";
		}
	
		StrSql += "ORDER BY [ItemCode] ASC";
		
		return con.selectData(StrSql);
	}
	
//	 public void closeConnecttion() throws Exception {
//    	 if (conn != null) {
//             conn.close();
//         }
//    }
	
	 public void DEFINE_ItemPrice() {
			rs = null;
			myDb = new SQLiteDBClass(getContext());
			db = myDb.getWritableDatabase();

			try {
				rs = selectItemPrice();
				while (rs.next()) {
					ContentValues Val = new ContentValues();
					Val.put("ItemPriceOID", rs.getString("OID"));

					Val.put("CarTypeOID", rs.getString("CarTypeOID"));
					Val.put("CarSizeOID", rs.getString("CarSizeOID"));
					Val.put("CarGroupOID", rs.getString("CarGroupOID"));
					Val.put("ItemOID", rs.getString("ItemOID"));
					Val.put("ItemPrice", rs.getString("ItemPrice"));

					Val.put("CarGroupName", rs.getString("CarGroupName"));
					Val.put("CarSizeName", rs.getString("CarSizeName"));
					Val.put("CarTypeName", rs.getString("CarTypeName"));
					Val.put("ItemCode", rs.getString("ItemCode"));
					Val.put("ItemName", rs.getString("ItemName"));

					Val.put("ItemGroupCode", rs.getString("ItemGroupCode"));
					Val.put("ItemGroupDesc", rs.getString("ItemGroupDesc"));
					Val.put("ItemGroupOID", rs.getString("ItemGroupOID"));
					Val.put("ItemGroupMainCode", rs.getString("ItemGroupMainCode"));
					Val.put("ItemGroupMainDesc", rs.getString("ItemGroupMainDesc"));

					Val.put("ItemGroupMainOID", rs.getString("ItemGroupMainOID"));
					Val.put("ItemUnitDesc", rs.getString("ItemUnitDesc"));

					db.insert(TABLE_ItemPrice, null, Val);
				}
				rs.close();
			} catch (Exception e) {
				Log.d("TABLE_ItemPrice", "Create Table not Successfully.");
			}
			Log.d("TABLE_ItemPrice", "Create Table Successfully.");
			db.close();
		}
	 
	 public String[][] SelectAll_ItemPrice(String ItemGroupOID,String ItemPriceOID,String CarSizeOID,String CarGroupOID) {
			try {
				String arrData[][] = null;
				myDb = new SQLiteDBClass(getContext());
				db = myDb.getWritableDatabase();
				String strSQL = "SELECT  ItemPriceOID As _id , * FROM "
						+ TABLE_ItemPrice;
				if (!ItemGroupOID.equals("")) {
					strSQL += " WHERE [ItemGroupOID] LIKE '" + ItemGroupOID + "' ";
				}
				if (!ItemPriceOID.equals("")) {
					strSQL += " WHERE [ItemPriceOID] LIKE '" + ItemPriceOID + "' ";
				}
				if (!CarSizeOID.equals("")) {
					strSQL += " AND [CarSizeOID] LIKE '" + CarSizeOID + "' ";
				}
				if (!CarGroupOID.equals("")) {
					strSQL += " AND [CarGroupOID] LIKE '" + CarGroupOID + "' ";
				}
				strSQL += " ORDER BY  CAST ([ItemPrice] AS DECIMAL) ";
				
				Cursor cursor = db.rawQuery(strSQL, null);
				if (cursor != null) {
					if (cursor.moveToFirst()) {
						arrData = new String[cursor.getCount()][cursor
								.getColumnCount()];
						int i = 0;
						do {
							arrData[i][0] = cursor.getString(1);
							arrData[i][1] = cursor.getString(2);
							arrData[i][2] = cursor.getString(3);
							arrData[i][3] = cursor.getString(4);
							arrData[i][4] = cursor.getString(5);
							arrData[i][5] = cursor.getString(6);
							arrData[i][6] = cursor.getString(7);
							arrData[i][7] = cursor.getString(8);
							arrData[i][8] = cursor.getString(9);
							arrData[i][9] = cursor.getString(10);
							arrData[i][10] = cursor.getString(11);
							arrData[i][11] = cursor.getString(12);
							arrData[i][12] = cursor.getString(13);
							arrData[i][13] = cursor.getString(14);
							arrData[i][14] = cursor.getString(15);
							arrData[i][15] = cursor.getString(16);
							arrData[i][16] = cursor.getString(17);
							arrData[i][17] = cursor.getString(18);

							i++;
						} while (cursor.moveToNext());
					}
				}
				cursor.close();

				return arrData;

			} catch (Exception e) {
				return null;
			}
		}

}
